# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

pkgname=stern
pkgdesc="Multi pod and container log tailing for Kubernetes"
pkgver=1.27.0
pkgrel=1
arch=('loong64' 'x86_64')
url="https://github.com/stern/stern"
license=('apache')
options=(!lto)
depends=('glibc')
makedepends=('go')
optdepends=('bash-completion: for bash completion support')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('71dd48d88d9ebb40e7c44c3a4d1aef54de6c58cb6f7d8535aacd8ad38bbf01e81574686377d904b22b6964c9394096e362671a5479c16ed6d55b7146ba988c51')
b2sums=('e3e6d825015657cd8fe9cc49f2327e45a5d948a2536db531b2981578feac5d94a9ad72a7c3459e5392a3efe08179881a51faf297f3ee03ec55a5e63c7dee3432')

build() {
  cd "${pkgname}-${pkgver}"

  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -mod=readonly -modcacherw"

  go build -v -x \
    -ldflags "-linkmode external -X \"github.com/stern/stern/cmd.version=${pkgver}\"" \
    -o "./out/${pkgname}"
}

check() {
  cd "${pkgname}-${pkgver}"

  go test -v ./...
}

package() {
  cd "${pkgname}-${pkgver}"

  install --verbose -D --mode=0755 "./out/${pkgname}" "${pkgdir}/usr/bin/${pkgname}"

  "${pkgdir}/usr/bin/${pkgname}" --completion=bash | install --verbose -D --mode=0644 /dev/stdin "${pkgdir}/usr/share/bash-completion/completions/${pkgname}"
  "${pkgdir}/usr/bin/${pkgname}" --completion=fish | install --verbose -D --mode=0644 /dev/stdin "${pkgdir}/usr/share/fish/vendor_completions.d/${pkgname}.fish"
  "${pkgdir}/usr/bin/${pkgname}" --completion=zsh | install --verbose -D --mode=0644 /dev/stdin "${pkgdir}/usr/share/zsh/site-functions/_${pkgname}"

  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
